Add a new CARGO_PKG_AUTHORS environment variable
authorMarcel Müller <neikos@neikos.email>
Thu, 10 Mar 2016 09:35:50 +0000 (10:35 +0100)
committerMarcel Müller <neikos@neikos.email>
Fri, 8 Apr 2016 18:34:55 +0000 (20:34 +0200)
This will allow crates to use the CARGO_PKG_AUTHORS env variable to get a colon
seperated list of the authors declared in the manifest.

Closes #2441

src/cargo/core/package.rs
src/cargo/ops/cargo_rustc/compilation.rs
src/doc/environment-variables.md
tests/test_cargo_compile.rs

index 703fcff83ca0d1af28023970a82c73f3073a7df2..c08fa0723cb7f0c6cf4f842b18de8ad9d5d070c8 100644 (file)
@@ -80,6 +80,7 @@ impl Package {
     pub fn summary(&self) -> &Summary { self.manifest.summary() }
     pub fn targets(&self) -> &[Target] { self.manifest().targets() }
     pub fn version(&self) -> &Version { self.package_id().version() }
+    pub fn authors(&self) -> &Vec<String> { &self.manifest.metadata().authors }
     pub fn publish(&self) -> bool { self.manifest.publish() }
 
     pub fn has_custom_build(&self) -> bool {
index b71c504c65a874fbd8375100122f52438e895f4b..f5fd87ddea2a4b3acee897a9f3ebfd694f668cdb 100644 (file)
@@ -120,6 +120,7 @@ impl<'cfg> Compilation<'cfg> {
            .env("CARGO_PKG_NAME", &pkg.name())
            .env("CARGO_PKG_DESCRIPTION", metadata.description.as_ref().unwrap_or(&String::new()))
            .env("CARGO_PKG_HOMEPAGE", metadata.homepage.as_ref().unwrap_or(&String::new()))
+           .env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
            .cwd(pkg.root());
         Ok(cmd)
     }
index 8f77b5c9122ffc6aff1afc3be345cc7b6c18df56..1f23ce68ecaef58e5fde7ecde54bdddbbd53a023 100644 (file)
@@ -45,6 +45,7 @@ let version = env!("CARGO_PKG_VERSION");
 * `CARGO_PKG_VERSION_MINOR` - The minor version of your package.
 * `CARGO_PKG_VERSION_PATCH` - The patch version of your package.
 * `CARGO_PKG_VERSION_PRE` - The pre-release version of your package.
+* `CARGO_PKG_AUTHORS` - Colon seperated list of authors from the manifest of your package.
 
 # Environment variables Cargo sets for build scripts
 
index 131f082094008a59fdd4301bec9dd0c992954b20..f761cd5fd1bc19e4a32fa78552ffcc2187a9235d 100644 (file)
@@ -784,6 +784,44 @@ test!(crate_env_vars {
                 execs().with_status(0));
 });
 
+test!(crate_authors_env_vars {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.1-alpha.1"
+            authors = ["wycats@example.com", "neikos@example.com"]
+        "#)
+        .file("src/main.rs", r#"
+            extern crate foo;
+
+            static AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");
+
+            fn main() {
+                let s = "wycats@example.com:neikos@example.com";
+                assert_eq!(AUTHORS, foo::authors());
+                println!("{}", AUTHORS);
+                assert_eq!(s, AUTHORS);
+            }
+        "#)
+        .file("src/lib.rs", r#"
+            pub fn authors() -> String {
+                format!("{}", env!("CARGO_PKG_AUTHORS"))
+            }
+        "#);
+
+    println!("build");
+    assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0));
+
+    println!("bin");
+    assert_that(process(&p.bin("foo")),
+                execs().with_stdout(&format!("wycats@example.com:neikos@example.com")));
+
+    println!("test");
+    assert_that(p.cargo("test").arg("-v"),
+                execs().with_status(0));
+});
+
 // this is testing that src/<pkg-name>.rs still works (for now)
 test!(many_crate_types_old_style_lib_location {
     let mut p = project("foo");